import java.sql.*;

public class Users {
    //数据库url、用户名和密码
    private static String username;
    private static String password;
    public void setUsername(String username) {Users.username = username;}
    public void setPassword(String password) {Users.password = password;}
    public boolean validate() {
        boolean result = false;
        try {
            String url = "jdbc:mysql://localhost:3306/db1";
            String user = "root";
            String pass = "cyx123456";
//1、注册JDBC驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
//2、获取数据库连接
            Connection connection = DriverManager.getConnection(url, user, pass);

//3、操作数据
            String sql = "select * from user where username=? and password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet resultSet = ps.executeQuery();//查询sql，获取结果集
            if (resultSet.next()) {
            result=true;
            System.out.println("PASS");
        } else {
            System.out.println("Not PASS");
        }//判断密码是否正确
//4、关闭结果集、数据库操作对象、数据库连接
        resultSet.close();
        connection.close();
        ps.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
        return result;
    }
}

